package com.mall.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mall.dao.OrderItemMapper;
import com.mall.dao.OrderMapper;
import com.mall.entity.PageResult;
import com.mall.pojo.order.Order;
import com.mall.pojo.order.OrderFull;
import com.mall.pojo.order.OrderItem;
import com.mall.service.goods.SkuService;
import com.mall.service.order.CartService;
import com.mall.service.order.OrderItemService;
import com.mall.service.order.OrderService;
import com.mall.util.IdWorker;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;

import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@DubboService
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    private OrderItemService orderItemService;

    private static final String TX_PGROUP_NAME="tx_order";
    Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);

    /**
     * 返回全部记录
     * @return
     */
    public List<Order> findAll() {
        return orderMapper.selectAll();
    }

    /**
     * 分页查询
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<Order> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Page<Order> orders = (Page<Order>) orderMapper.selectAll();
        return new PageResult<Order>(orders.getTotal(),orders.getResult());
    }

    /**
     * 条件查询
     * @param searchMap 查询条件
     * @return
     */
    public List<Order> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return orderMapper.selectByExample(example);
    }

    @Override
    public PageResult<Order> findPage(Map<String, Object> searchMap, int page, int size) {
        return null;
    }


    /**
     * 根据Id查询
     * @param id
     * @return
     */
    public Order findById(String id) {
        return orderMapper.selectByPrimaryKey(id);
    }

    @Autowired
    private CartService cartService;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private OrderItemMapper orderItemMapper;


    /**
     * 新增
     * 1、获取选中购物车信息
     * 2、扣减库存
     * 3、保存订单主体
     * 4、保存订单明细（打折比例优惠金额）
     * 5、清除购物车
     */
    @Override
    public Map<String, Object> add(Order order) {
        Map<String,Object> retMap =new HashMap<>();
        //获取购物车(刷新单价)
        List<Map<String, Object>> orderItemList = cartService.findNewOrderItemList(order.getUsername());
        List<OrderItem> orderItems = orderItemList.stream()
                .filter(cart -> (boolean) cart.get("checked"))
                .map(cart -> (OrderItem) cart.get("item"))
                .collect(Collectors.toList());
        //合计数计算
        IntStream numStream = orderItems.stream().mapToInt(OrderItem::getNum);
        IntStream moneyStream = orderItems.stream().mapToInt(OrderItem::getMoney);
        int totalNum = numStream.sum();//总数量
        int totalMoney = moneyStream.sum();//订单总金额
        int preferential = cartService.preferential(order.getUsername());//计算优惠金额

        //加工订单主表
        order.setId(idWorker.nextId()+"");
        order.setTotalNum(totalNum);//总数量
        order.setTotalMoney(totalMoney);//总金额
        order.setPreMoney(preferential);//优惠金额
        order.setPayMoney(totalMoney-preferential);//支付金额=总金额+优惠金额
        order.setCreateTime(new Date());//订单创建日期
        order.setOrderStatus("0"); // 订单状态
        order.setPayStatus("0"); // 支付状态：未支付
        order.setConsignStatus("0");   //发货状态：未发货
        double proportion = (double)order.getPayMoney()/totalMoney;
        //加工订单明细
        for(OrderItem orderItem:orderItems){
            orderItem.setOrderId(order.getId());//订单主表ID
            orderItem.setId(idWorker.nextId()+"");
            orderItem.setPayMoney( (int)(orderItem.getMoney()* proportion) );//支付金额
        }
        order.setOrderItemList(orderItems);
        retMap.put("orderData",order);

        //发送半消息
        CountDownLatch latch =new CountDownLatch(1);
        Map map =new HashMap();
        map.put("order",order);
        map.put("latch",latch);
        Message<Order> msg = MessageBuilder.withPayload(order).setHeader(RocketMQHeaders.KEYS, "KEY_" + 2).build();
        SendResult sendResult = rocketMQTemplate.sendMessageInTransaction(TX_PGROUP_NAME, "tx_order_msg" + ":" + "order", msg, map);

        try {
            latch.await();
            rocketMQTemplate.syncSend("quene_delete_cart",order.getUsername());
        }catch (Exception e){
            //记录loginfo日志
            logger.error("购物车清除失败，订单号："+order.getId()+"用户名："+order.getUsername()+"购物车列表："+ JSON.toJSONString(orderItems));
        }

        retMap.put("ordersn", order.getId() );
        retMap.put("money",order.getPayMoney());
        return retMap;
    }

    @Override
    public void insertOrder(Order order) {
        orderMapper.insert(order);
        for (OrderItem orderItem : order.getOrderItemList()) {
            orderItemMapper.insert(orderItem);
        }
    }

    /**
     * 修改
     * @param order
     */
    public void update(Order order) {
        orderMapper.updateByPrimaryKeySelective(order);
    }

    /**
     *  删除
     * @param id
     */
    public void delete(String id) {
        orderMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void batchSend(List<Order> orders) {
        //判断运单号和物流公司是否为空
        for(Order order :orders){
            if(order.getShippingCode()==null || order.getShippingName()==null){
                throw new RuntimeException("请选择快递公司和填写快递单号");
            }
        }
        //循环订单
        for(Order order :orders){
            order.setOrderStatus("3");//订单状态  已发货
            order.setConsignStatus("2"); //发货状态  已发货
            order.setConsignTime(new Date());//发货时间
            orderMapper.updateByPrimaryKeySelective(order);

//            saveOrderLog(order,"admin");
        }
    }

    @Override
    public void orderTimeOutLogic() {

    }

    @Override
    public void updatePayStatus(String orderId, String transactionId) {

    }

    @Override
    public OrderFull findOrderFullById(String id) {
        return null;
    }

    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap){
        Example example=new Example(Order.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 支付类型，1、微信支付，2、支付宝 3、银联支付 、4 货到付款
            if(searchMap.get("payType")!=null && !"".equals(searchMap.get("payType"))){
                criteria.andLike("payType","%"+searchMap.get("payType")+"%");
            }
            // 物流名称
            if(searchMap.get("shippingName")!=null && !"".equals(searchMap.get("shippingName"))){
                criteria.andLike("shippingName","%"+searchMap.get("shippingName")+"%");
            }
            // 物流单号
            if(searchMap.get("shippingCode")!=null && !"".equals(searchMap.get("shippingCode"))){
                criteria.andLike("shippingCode","%"+searchMap.get("shippingCode")+"%");
            }
            // 用户名称
            if(searchMap.get("username")!=null && !"".equals(searchMap.get("username"))){
                criteria.andLike("username","%"+searchMap.get("username")+"%");
            }
            // 买家留言
            if(searchMap.get("buyerMessage")!=null && !"".equals(searchMap.get("buyerMessage"))){
                criteria.andLike("buyerMessage","%"+searchMap.get("buyerMessage")+"%");
            }
            // 是否评价
            if(searchMap.get("buyerRate")!=null && !"".equals(searchMap.get("buyerRate"))){
                criteria.andLike("buyerRate","%"+searchMap.get("buyerRate")+"%");
            }
            // 收货人
            if(searchMap.get("receiverContact")!=null && !"".equals(searchMap.get("receiverContact"))){
                criteria.andLike("receiverContact","%"+searchMap.get("receiverContact")+"%");
            }
            // 收货人手机
            if(searchMap.get("receiverMobile")!=null && !"".equals(searchMap.get("receiverMobile"))){
                criteria.andLike("receiverMobile","%"+searchMap.get("receiverMobile")+"%");
            }
            // 收货人地址
            if(searchMap.get("receiverAddress")!=null && !"".equals(searchMap.get("receiverAddress"))){
                criteria.andLike("receiverAddress","%"+searchMap.get("receiverAddress")+"%");
            }
            // 订单来源：1:web，2：app，3：微信公众号，4：微信小程序  5 H5手机页面
            if(searchMap.get("sourceType")!=null && !"".equals(searchMap.get("sourceType"))){
                criteria.andLike("sourceType","%"+searchMap.get("sourceType")+"%");
            }
            // 交易流水号
            if(searchMap.get("transactionId")!=null && !"".equals(searchMap.get("transactionId"))){
                criteria.andLike("transactionId","%"+searchMap.get("transactionId")+"%");
            }
            // 订单状态
            if(searchMap.get("orderStatus")!=null && !"".equals(searchMap.get("orderStatus"))){
                criteria.andLike("orderStatus","%"+searchMap.get("orderStatus")+"%");
            }
            // 支付状态
            if(searchMap.get("payStatus")!=null && !"".equals(searchMap.get("payStatus"))){
                criteria.andLike("payStatus","%"+searchMap.get("payStatus")+"%");
            }
            // 发货状态
            if(searchMap.get("sendStatus")!=null && !"".equals(searchMap.get("sendStatus"))){
                criteria.andLike("sendStatus","%"+searchMap.get("sendStatus")+"%");
            }
            // 退货状态
            if(searchMap.get("returnStatus")!=null && !"".equals(searchMap.get("returnStatus"))){
                criteria.andLike("returnStatus","%"+searchMap.get("returnStatus")+"%");
            }
            // 退款状态
            if(searchMap.get("refundStauts")!=null && !"".equals(searchMap.get("refundStauts"))){
                criteria.andLike("refundStauts","%"+searchMap.get("refundStauts")+"%");
            }
            // 是否删除
            if(searchMap.get("isDelete")!=null && !"".equals(searchMap.get("isDelete"))){
                criteria.andLike("isDelete","%"+searchMap.get("isDelete")+"%");
            }

            // 数量合计
            if(searchMap.get("totalNum")!=null ){
                criteria.andEqualTo("totalNum",searchMap.get("totalNum"));
            }
            // 金额合计
            if(searchMap.get("totalMoney")!=null ){
                criteria.andEqualTo("totalMoney",searchMap.get("totalMoney"));
            }
            // 优惠金额
            if(searchMap.get("preMoney")!=null ){
                criteria.andEqualTo("preMoney",searchMap.get("preMoney"));
            }
            // 邮费
            if(searchMap.get("postFee")!=null ){
                criteria.andEqualTo("postFee",searchMap.get("postFee"));
            }
            // 实付金额
            if(searchMap.get("payMoney")!=null ){
                criteria.andEqualTo("payMoney",searchMap.get("payMoney"));
            }
            // 根据  id 数组查询 查询
            if(searchMap.get("ids")!=null   ){
                criteria.andIn("id", Arrays.asList((Integer[])searchMap.get("ids")));
            }
            // 开始时间<=
            if(searchMap.get("createTime")!=null && !"".equals(searchMap.get("createTime"))){
                criteria.andLessThanOrEqualTo("createTime",searchMap.get("createTime"));
            }
        }
        return example;
    }

    /**
     * 构建查询条件
     * @param searchMap
     * @return
     */
    private Example createOrderItemExample(Map<String, Object> searchMap){
        Example example=new Example(OrderItem.class);
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            // 标题
            if(searchMap.get("title")!=null && !"".equals(searchMap.get("title"))){
                criteria.andLike("title","%"+searchMap.get("title")+"%");
            }
            // 图片地址
            if(searchMap.get("picPath")!=null && !"".equals(searchMap.get("picPath"))){
                criteria.andLike("picPath","%"+searchMap.get("picPath")+"%");
            }

            // 单价
            if(searchMap.get("price")!=null ){
                criteria.andEqualTo("price",searchMap.get("price"));
            }
            // 数量
            if(searchMap.get("num")!=null ){
                criteria.andEqualTo("num",searchMap.get("num"));
            }
            // 总金额
            if(searchMap.get("money")!=null ){
                criteria.andEqualTo("money",searchMap.get("money"));
            }
            // 重量
            if(searchMap.get("weight")!=null ){
                criteria.andEqualTo("weight",searchMap.get("weight"));
            }
            // orderId
            if(searchMap.get("orderId")!=null ){
                criteria.andEqualTo("orderId",searchMap.get("orderId"));
            }

        }
        return example;
    }

}
